package com.coolshark.shop.mapper;

import com.coolshark.shop.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{url},#{price},#{oldPrice},#{saleCount},#{num},0,#{created},#{categoryId})")
    void insert(Product product);

    @Select("select id,title,price,old_price,sale_count,url from product")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> select();

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);

    @Delete("delete from product where id=#{id}")
    void deleteById(int id);

    @Select("select id,title,url,price,old_price,sale_count from product")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectIndex();

    @Select("select id,title,sale_count from product order by sale_count desc limit 0,6")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectTop();

    @Select("select id,title,url,price,old_price,num,created,sale_count,view_count,category_id from product where id=#{id}")
//    @Result(column = "old_price",property = "oldPrice")
//    @Result(column = "sale_count",property = "saleCount")
//    @Result(column = "view_count",property = "viewCount")
    Product selectById(int id);

    @Update("update product set view_count=vie_count+1 where id=#{id}")
    void updateViewCount(int id);

    @Select("select id,title,url,price,old_price,sale_count from product where category_id=#{id}")
    List<Product> selectByCid(int id);

    @Select("select id,title,url,price,old_price,sale_count from product where title like concat('%',#{wd},'%')")
    List<Product> selectByWd(String wd);

    @Delete("delete from product where category_id=#{id}")
    void deleteByCid(int id);

    @Update("update product set title=#{title},price=#{price},old_price=#{oldPrice},num=#{num},sale_count=#{saleCount},url=#{url},category_id=#{categoryId} where id=#{id}")
    void update(Product product);
}
